#include "ObGrass.h"
#include "D3DApp.h"
#include "D3DUtil.h"

IDirect3DTexture9* ObGrass::mTex = 0;

ObGrass::ObGrass(){
	setId(Grass);
	mInfo	= 0;
	mSpritePos.x = 20;
	mSpritePos.y = 10;
	mCurrentState = 0;
	timeAcum = 0.0f;
}

ObGrass::~ObGrass(){

}

bool ObGrass::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		mWidth	= 32;
		mHeight = 16;
		mCenter = D3DXVECTOR3(mWidth/2,mHeight/2,0.0f);
		return true;
	}else{
		return false;
	}
}

bool ObGrass::checkType(int obID){
	if(obID == Grass){
		return true;
	}else{
		return false;
	}
}

void ObGrass::update(float dt){
	timeAcum+=dt;

	if(timeAcum >= 0.5f){
		timeAcum = 0.0f;
		mCurrentState++;
		if(mCurrentState>1){
			mCurrentState = 0;
		}
		mSpritePos.y = 10.0f + 20*mCurrentState;
	}
}
void ObGrass::render(ID3DXSprite* mSprite){
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x,  mPos.y - gCamera->getPos().y, 0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
}

bool ObGrass::loadTexture(){
	if(mTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/Grass.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex));
		
		return true;
	}else{
		return false;
	}
}

bool ObGrass::releaseTexture(){
	if(mTex!=0){
		ReleaseCOM(mTex);
		return true;
	}else{
		return false;
	}
}